<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */

    /**
     * The Session manager of the application.
     *
     * @package    kassiop.managers
     * @subpackage sessions
     *
     * @author NairuS
     * @version 1.3 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     */
     class SessionManager
     {
        /**
         * The name of the Authentified session var.
         *
         * @var string
         */
        const AUTHENTIFIED = "isAuthentified" ;

        /**
         * Defines the current cache limiter.
         *
         * @see http://fr.php.net/manual/fr/session.configuration.php#ini.session.cache-limiter
         * @var string
         */
        const SESSION_CACHE_LIMITER = "nocache" ; // the values allowed are : none, nocache, private, private_no_expire, public. By default, it must be nocache.

        /**
         * The numbers of minutes for session expiration.
         *
         * @var integer
         */
        const SESSION_EXPIRE = 1440 ; // 24 hours.


        /**
         * The name of the User session name.
         *
         * @var string
         */
        const USER_INFOS = "userInfos" ;

        /**
         * Builds the name of the session
         * @param  string $prefixe
         * @param  string $varName
         * @return string
         */
        public static function buildSessionVar( $prefixe, $varName )
        {
            return $prefixe . '.' . $varName ;
        }

        /**
         * Defines if the user is authentified.
         *
         * @param  string $prefixe
         * @return boolean
         */
        public static function isAuthentified( $prefixe )
        {
            return self::isValid() && self::read( self::buildSessionVar( $prefixe, SessionManager::AUTHENTIFIED ) ) === true ;
        }

        /**
         * Defines if the session is valid.
         *
         * @return boolean
         */
        public static function isValid()
        {
            return isset( $_SESSION ) ;
        }

        /**
         * Destroy the session.
         */
        public static function destroy()
        {
            session_unset() ;
            $_SESSION[SessionManager::AUTHENTIFIED] = false ;
        }

        /**
         * Check if the session named exists and start it.
         *
         * @param  string $name The name of the session to open.
         * @return mixed        The value of the Session var if it exists.
         */
        public static function open()
        {
            if( !isset( $_SESSION ) )
            {
                self::setExpire();
            }
            header( 'P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"' ) ;
            session_start() ;
        }

        /**
         * Return the session var
         *
         * @param  string $name The name of the session var to return.
         * @return mixed        The session var or null if doesn't exist.
         */
        public static function read( $name )
        {
            return isset( $_SESSION[$name] ) ? $_SESSION[$name] : null ;
        }

        /**
         * Sets the expire time of the session.
         */
        public static function setExpire()
        {
            session_cache_limiter( self::SESSION_CACHE_LIMITER ) ;
            ( session_cache_expire( self::SESSION_EXPIRE ) * 60 ) + time() ;
        }

        /**
         * Set the session authentified of the user.
         *
         * @return void
         */
        public static function setIsAuthentified( $prefixe )
        {
            $_SESSION[self::buildSessionVar( $prefixe, SessionManager::AUTHENTIFIED )] = true ;
        }

        /**
         * Removes a session var if it exists.
         *
         * @param  string $var
         * @return void
         */
        public static function remove( $var )
        {
            if( self::read( $var ) !== null )
            {
                session_unregister( $var ) ;
            }
        }

        /**
         * Write the session var.
         *
         * @param  string $name
         * @param  mixed  $data
         * @return void
         */
        public static function write( $name , $value )
        {
            $_SESSION[$name] = $value ;
        }
     }
?>